package org.example.c_0_002;

import org.junit.Test;

import java.util.ArrayList;

/**
 * volatile不能保证原子性
 */
public class T_Volatile_Atomicity {
    private volatile int count = 0;

    private void m(){
        for (int i = 0; i < 10000; i++) {
            count++;
        }
    }

    @Test
    public void run(){
        ArrayList<Thread> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(new Thread(this::m));
        }
        list.forEach(obj -> obj.start());
        list.forEach(obj -> {
            try {
                obj.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        System.out.println(count);
    }
}
